package com.easyj.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import org.apache.commons.lang3.StringUtils;


public class DateTimeUtil {
	public static final SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat FORMAT_YYYYMMDDHHMMSS17 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static final SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat FORMAT_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
	public static final SimpleDateFormat FORMAT_YYYY = new SimpleDateFormat("yyyy");
	public static final SimpleDateFormat FORMAT_YYYY_MM = new SimpleDateFormat("yyyy-MM");
	public static Calendar calendar=Calendar.getInstance();
	
	/**
	 *  加一个月
	 * @param date
	 * @return
	 */
	public static Date add_1_Month(Date date) {
		if(date!=null){
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, 1);
			return calendar.getTime();
		}else {
			return null;
		}
		
	}
	/**
	 * 当前月的第一天
	 */
	public static Date getMonthFirstDay(Date date) {
		if(date!=null){
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			return calendar.getTime();
		}else {
			return null;
		}
		
	}
	/**
	 * 加一天
	 * @param date
	 * @return
	 */
	public static Date  add_1_Day(Date date){
		if(date!=null){
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			return calendar.getTime();
		}else {
			return null;
		}
	}
	/**
	 * 减一天
	 * @param date
	 * @return
	 */
	public static Date  sub_1_Day(Date date){
		if(date!=null){
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -1);
			return calendar.getTime();
		}else {
			return null;
		}
	}
	
	public static String getDateTimeStr(Date datetime) {
		if(datetime==null) return "";
		return FORMAT_YYYY_MM_DD_HH_MM_SS.format(datetime);
	}
	
	public static String getDateStr(Date datetime) {
		if(datetime==null) return "";
		return FORMAT_YYYY_MM_DD.format(datetime);
	}
	
	public static String getDateStr8(Date datetime) {
		if(datetime==null) return "";
		return FORMAT_YYYYMMDD.format(datetime);
	}

	public static String getDateTimeStr17(Date datetime) {
		if(datetime==null) return "";
		return FORMAT_YYYYMMDDHHMMSS17.format(datetime);
	}
	
	public static String getYearStr(Date datetime){
		if(datetime==null) return "";
		return FORMAT_YYYY.format(datetime);
	}
	
	public static String getMonthStr(Date datetime){
		if(datetime==null) return "";
		return FORMAT_YYYY_MM.format(datetime);
	}
	
	public static Date toDate(String datestr) throws Exception{
		if(StringUtils.isBlank(datestr)){
			return null;
		}
		
		try {
			return FORMAT_YYYY_MM_DD.parse(datestr);
			
		} catch (ParseException e) {
			throw new Exception("日期格式不是：yyyy-MM-dd");
		}
	}
	
	public static Date toDateSmart(String datestr) throws Exception{
		if(StringUtils.isBlank(datestr)){
			return null;
		}
		if(datestr.length()<=8){
			try {
				return FORMAT_YYYYMMDD.parse(datestr);
				
			} catch (ParseException e) {
				throw new Exception("日期格式不是：yyyyMMdd");
			}
		}
		try {
			return FORMAT_YYYY_MM_DD.parse(datestr);
			
		} catch (ParseException e) {
			throw new Exception("日期格式不是：yyyy-MM-dd");
		}
	}
	
	public static Date toDateTime(String datestr) throws Exception{
		if(StringUtils.isBlank(datestr)){
			return null;
		}
		
		try {
			return FORMAT_YYYY_MM_DD_HH_MM_SS.parse(datestr);
			
		} catch (ParseException e) {
			throw new Exception("日期时间格式不是：yyyy-MM-dd HH:mm:ss");
		}
	}
	
	public static Date toDateTimeSmart(String datestr) throws Exception{
		if(StringUtils.isBlank(datestr)){
			return null;
		}
		if(datestr.length()<=10){
			try {
				return FORMAT_YYYY_MM_DD.parse(datestr);
				
			} catch (ParseException e) {
				throw new Exception("日期格式不是：yyyy-MM-dd");
			}
		}else {
			try {
				return FORMAT_YYYY_MM_DD_HH_MM_SS.parse(datestr);
				
			} catch (ParseException e) {
				throw new Exception("日期时间格式不是：yyyy-MM-dd HH:mm:ss");
			}
		}	
	}
}
